﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tool.Helper
{
    public class ShutdownHelper
    {
        /// <summary>
        /// 立即关机
        /// </summary>
        public static void ShutdownNow()
        {
            try
            {
                Process.Start("shutdown", "/s /t 0 /f");
                // 或者使用以下方式，效果相同
                // Process.Start(new ProcessStartInfo("cmd", "/c shutdown /s /t 0 /f") { CreateNoWindow = true, UseShellExecute = false });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"关机失败: {ex.Message}");
                // 在实际应用中，可能需要处理权限不足等问题
            }
        }

        /// <summary>
        /// 指定秒数后重启，默认120秒
        /// </summary>
        public static void RebootInOneMinute(int seconds=120)
        {
            try
            {
                Process.Start("shutdown", "/r /t "+ seconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"重启命令失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 注销当前用户
        /// </summary>
        public static void Logoff()
        {
            try
            {
                Process.Start("shutdown", "/l");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"注销失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 在指定秒后关机
        /// </summary>
        public static void ShutdownNowDelaySecond(int seconds=120)
        {
            try
            {
                Process.Start("shutdown", "/s /t "+ seconds + " /f");
                // 注意：消息文本中的引号需要用双引号包围
                //Process.Start("shutdown", "/s /t "+ seconds + " /c \"系统将在"+ seconds+"秒后自动关机，请保存您的工作！\"");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"定时关机失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 取消已计划的关机
        /// </summary>
        public static void CancelShutdown()
        {
            try
            {
                Process.Start("shutdown", "/a");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"取消关机失败: {ex.Message}");
            }
        }
    }
}
